//
//  UIBarButtonItem+Extension.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/12.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    
    /*
     便利构造函数:方便创建对象
     1.在分类里面只能使用便利构造函数
     2.便利构造函数最终需要调用一个指定构造函数
     3.调用便利构造函数,要保证调用的这个便利构造函数会调用到指定的构造函数
     4.构造函数,不会省略第一个外部参数名
     */
    convenience init(imgName:String,selectedImgName:String, target: AnyObject?, action: Selector)
    {
        let img = UIImage(named: imgName)
        let selectedImg = UIImage(named: selectedImgName)
        
        let btn = UIButton()
        btn.setImage(img, forState: UIControlState.Normal)
        btn.setImage(selectedImg, forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //创建UIBarButtonItem对象设置customView
        //public convenience init(customView: UIView) ,它内部会去调指定构造函数
        self.init(customView: btn)
    }
    
    
    
    /// 使用类方法
    class func createBarButtonItem(imageName: String) -> UIBarButtonItem {
        
        // 创建按钮
        let button = UIButton()
        
        // 设置按钮
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        // 创建UIBarButtonItem设置customView,返回回去
        return UIBarButtonItem(customView: button)
    }
    
    
    
    
    
    
    
    
}











